import { API_URL, MyAjax, upFileAjax, CODE } from '../config';
import { function_getuserinfo } from './member';

export const add_project = 'add_project';
export const get_project = 'get_project';
export const set_project_name = 'set_project_name';
export const set_project_icon = 'set_project_icon';
export const set_project_notify = 'set_project_notify';

export function function_set_project_icon (icon, projectId, cb, dispatch) {
  upFileAjax(API_URL.setProjectIcon, {
    projectId,
    icon: icon
  }, result => {
    if (result.code === CODE.SUCCESS) {
      cb(result.data);
      dispatch({
        type: set_project_icon,
        id: projectId,
        icon: result.data
      });
    } else {
      alert(result.message);
    }
  })
}

export function function_set_project_notify (chatType, chatHookUrl, notifySecret, projectId, dispatch, cb) {
  MyAjax(API_URL.setProjectNotify, {
    projectId,
    chatType,
    hookUrl: chatHookUrl,
    notifySecret,
  }, result => {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: set_project_notify,
        id: projectId,
        chatType,
        chatHookUrl
      });
      cb();
    }
  })
}

export function function_set_project_name (name, projectId, dispatch, cb) {
  MyAjax(API_URL.setProjectName, {
    projectId,
    name
  }, result => {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: set_project_name,
        id: projectId,
        name
      });
      cb();
    }
  })
}

export function function_get_project (projectId, dispatch, cb) {
  MyAjax(API_URL.getProject, {
    projectId
  }, result => {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: get_project,
        id: result.data.id,
        name: result.data.name,
        icon: result.data.icon,
        chatType: result.data.chatType,
        chatHookUrl: result.data.chatHookUrl,
        fatAdmin: result.data.fatAdmin,
        fatDeveloper: result.data.fatDeveloper,
        fatTester: result.data.fatTester
      });
      cb(result.data.fatAdmin, result.data.fatTester, result.data.fatDeveloper, result.data.chatType);
    }
  })
}

/**
 * 添加项目
 * @param {*} name 
 * @param {*} chatType 
 * @param {*} notifyUrl 
 * @param {*} dispatch 
 */
export function function_add_project (name, chatType, notifyUrl, notifySecret, dispatch) {
  MyAjax(API_URL.addProject, {
    name: name,
    chatType,
    notifyUrl,
    notifySecret
  }, result => {
    if (result.code === CODE.SUCCESS) {
      let projectId = result.data;
      dispatch({
        type: add_project,
        id: projectId,
        name: name
      });
      function_getuserinfo(dispatch);
    }
  });
}